package com.sky.happygos.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sky.happygos.R;
import com.sky.happygos.base.BaseA;
import com.sky.happygos.base.ImagePath;
import com.sky.happygos.common.LoadView;
import com.sky.happygos.event.GoodsE;
import com.sky.happygos.model.Goods;
import com.sky.happygos.present.GoodsP;
import com.sky.happygos.present.UserP;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import static com.sky.happygos.ui.activity.CreateGoodsA.EXTRA_CREATE_GOODSID;
import static com.sky.happygos.ui.activity.CreateGoodsA.EXTRA_CREATE_SHOPID;


/**
 * Created by qqqqqqq on 2017/4/15.
 */
@EActivity(R.layout.activity_goods_detail)
public class GoodsDetailA extends BaseA {
    public static final String EXTRA_GOODS_ID  = "EXTRA_GOODS_ID";

    @Bean
    GoodsP goodsP;

    Goods goods;

    @Extra(EXTRA_GOODS_ID)
    String goodsId;

    @ViewById(R.id.loadView)
    LoadView loadView;
    @ViewById(R.id.sdv_goods_detail_icon)
    SimpleDraweeView icon;
    @ViewById(R.id.tv_goods_detail_name)
    TextView name;
    @ViewById(R.id.tv_goods_detail_price)
    TextView price;
    @ViewById(R.id.tv_goods_detail_buynum)
    TextView count;
    @ViewById(R.id.tv_goods_detail_info)
    TextView info;

    @AfterViews
    public void init(){
        loadView.start();
        goodsP.getGoodsById(goodsId);
    }

    @Click(R.id.header_left)
    public void back(){
        finish();
    }

    @Click(R.id.rl_goods_detail_edit)
    public void edit(){
        CreateGoodsA_.intent(this).extra(EXTRA_CREATE_GOODSID,goods.id).start();
    }

    @Subscribe
    public void goodsResult(GoodsE e){
        loadView.dismiss();
        if(e.isSuccess){
            goods = e.data;
            refreshView();
        }else{
            Toast.makeText(this,"获取商品失败",Toast.LENGTH_SHORT).show();
        }
    }

    public void refreshView(){
        icon.setImageURI(ImagePath.to(goods.icon));
        name.setText(goods.name);
        info.setText(goods.info);
        price.setText(String.format("%.2f", goods.price));
        count.setText(String.format("销量 %d 库存 %d",0,goods.count));

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        goodsP.getGoodsById(goodsId);
    }
}
